package JZOffer;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

// https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/?envType=study-plan&id=lcof&plan=lcof&plan_progress=xxj1ozje
// 从上到下打印二叉树
public class Day006_1 {
    public int[] levelOrder(TreeNode root) {
        int[] arr = new int[1000];
        int index = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int count = 0;
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node != null) {
                queue.offer(node.left);
                queue.offer(node.right);
                arr[index++] = node.val;
                count++;
            }
        }
        return Arrays.copyOfRange(arr, 0, count);
    }
}
